package com.rabbit.eap.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbit.eap.common.lang.StringUtils;
import com.rabbit.eap.framework.common.api.ApiResult;
import com.rabbit.eap.framework.common.enums.MsgEnum;
import com.rabbit.eap.framework.common.service.impl.BaseServiceImpl;
import com.rabbit.eap.framework.core.pagination.PageInfo;
import com.rabbit.eap.framework.core.pagination.Paging;
import com.rabbit.eap.system.dto.SysConfigDTO;
import com.rabbit.eap.system.entity.SysConfig;
import com.rabbit.eap.system.mapper.SysConfigMapper;
import com.rabbit.eap.system.param.SysConfigPageParam;
import com.rabbit.eap.system.service.ISysConfigService;
import com.rabbit.eap.system.vo.SysConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * @projectName: eap
 * @packageName: com.rabbit.eap.system.service.impl
 * @className SysConfigServiceImpl
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/06/06 23:34:01
 * <p>Description:
 * 参数配置表 服务实现类
 * </p>

 */
@Slf4j
@Service
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * @param sysConfigDTO:sysConfigDTO数据传输对象
     * @throws Exception
     * @Description: 添加参数配置表
     * @methodName: saveSysConfig
     * @return: ApiResult
     * @date: 2020/06/06 23:34:01
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult saveSysConfig(SysConfigDTO sysConfigDTO) throws Exception {
        SysConfig sysConfig = sysConfigDTO.convert(SysConfig.class);
        boolean flag = this.checkConfigKeyUnique(sysConfigDTO);
        if (flag) {
            return ApiResult.fail("参数键名[" + sysConfigDTO.getConfigKey() + "]" + MsgEnum.IS_EXIST.getDesc());
        }
        boolean bool = super.save(sysConfig);
        if (bool) {
            return ApiResult.ok(MsgEnum.ADD_SUCCESS.getDesc());
        } else {
            return ApiResult.fail(MsgEnum.ADD_FAIL.getDesc());
        }
    }

    /**
     * @param sysConfigDTO:sysConfigDTO数据传输对象
     * @throws Exception
     * @Description 修改参数配置表
     * @methodName: modifySysConfig
     * @return: ApiResult
     * @date: 2020/06/06 23:34:01
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult modifySysConfig(SysConfigDTO sysConfigDTO) throws Exception {
        SysConfig sysConfig = sysConfigDTO.convert(SysConfig.class);
        boolean flag = this.checkConfigKeyUnique(sysConfigDTO);
        if (flag) {
            return ApiResult.fail("参数键名[" + sysConfigDTO.getConfigKey() + "]" + MsgEnum.IS_EXIST.getDesc());
        }
        boolean bool = super.updateById(sysConfig);
        if (bool) {
            return ApiResult.ok(MsgEnum.UPDATE_SUCCESS.getDesc());
        } else {
            return ApiResult.fail(MsgEnum.UPDATE_FAIL.getDesc());
        }
    }

    /**
     * @param ids:ids集合
     * @throws Exception
     * @Description 删除参数配置表
     * @methodName: removeSysConfig
     * @return: ApiResult
     * @date: 2020/06/06 23:34:01
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult removeSysConfig(Long[] ids) throws Exception {
        boolean bool = super.removeByIds(Arrays.asList(ids));
        if (bool) {
            return ApiResult.ok(MsgEnum.DELETE_SUCCESS.getDesc());
        } else {
            return ApiResult.fail(MsgEnum.DELETE_FAIL.getDesc());
        }
    }

    /**
     * @param id: id
     * @throws Exception
     * @Description 根据Id获取参数配置表详情
     * @methodName: getSysConfigInfo
     * @return: ApiResult
     * @date: 2020/06/06 23:34:01
     * @author: rabbit
     */
    @Override
    public ApiResult getSysConfigById(Long id) throws Exception {
        if (ObjectUtil.isNull(id)) {
            return ApiResult.fail(MsgEnum.PARAM_NOT_NULL.getDesc());
        }
        SysConfig sysConfig = super.getById(id);
        SysConfigVO sysConfigVO = sysConfig.convert(SysConfigVO.class);
        return ApiResult.ok(sysConfigVO);
    }

    /**
     * @param sysConfigPageParam
     * @throws Exception
     * @Description 参数配置表分页列表
     * @methodName: getSysConfigPageList
     * @return: ApiResult
     * @date: 2020/06/06 23:34:01
     * @author: rabbit
     */
    @Override
    public ApiResult getSysConfigPageList(SysConfigPageParam sysConfigPageParam) throws Exception {
        Page<SysConfig> page = new PageInfo<>(sysConfigPageParam, OrderItem.desc("create_time"));
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(sysConfigPageParam.getConfigKey()), SysConfig::getConfigKey, sysConfigPageParam.getConfigKey())
                .like(StringUtils.isNotBlank(sysConfigPageParam.getConfigName()), SysConfig::getConfigName, sysConfigPageParam.getConfigName())
                .eq(StringUtils.isNotBlank(sysConfigPageParam.getConfigType()),SysConfig::getConfigType,sysConfigPageParam.getConfigType());

        IPage<SysConfigVO> iPage = sysConfigMapper.selectPage(page, queryWrapper)
                .convert(sysConfig -> sysConfig.convert(SysConfigVO.class));
        Paging<SysConfigVO> paging = new Paging<SysConfigVO>(iPage);
        return ApiResult.ok(paging);
    }

    /**
     * @param configKey: 参数键名
     * @Description: 根据参数键名查询参数值
     * @methodName: getSysConfigByKey
     * @return: com.rabbit.eap.framework.common.api.ApiResult
     * @date: 2020/6/12 22:32
     * @author: rabbit
     */
    @Override
    public SysConfig getSysConfigByKey(String configKey) {
        SysConfig sysConfig = sysConfigMapper.selectOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, configKey));
        return sysConfig;
    }

    /**
     * @param config: 参数配置信息
     * @Description: 校验参数键名是否唯一
     * @methodName: checkConfigKeyUnique
     * @return: boolean
     * true则存在 false则不存在
     * @date: 2020/6/12 22:24
     * @author: rabbit
     */
    private boolean checkConfigKeyUnique(SysConfigDTO config) {
        Long configId = ObjectUtil.isEmpty(config.getId()) ? -1L : config.getId();
        SysConfig sysConfig = sysConfigMapper.selectOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, config.getConfigKey()));
        if (ObjectUtil.isNotNull(sysConfig) && sysConfig.getId().longValue() != configId.longValue()) {
            return true;
        }
        return false;
    }
}
